const { Router } = require("express");
const path = require("path");
const router = new Router();

const Update = require("../model/Recipe");
const Works = require("../model/Works");


router.use(require('./auths.js'));

// 文件解析
const multer = require('multer');

// 图片上传接口
const img = multer.diskStorage({
    destination: './api/img',
    filename: function (req, file, cb) {
        let ext = path.extname(file.originalname);
        cb(null, file.fieldname + Date.now() + ext)
    }
})
const imgs = multer({ storage: img })
router.post("/img", imgs.any(), (req, res) => {
    let files = req.files;
    // let url = 'http://127.0.0.1:8000' + "/img/" + files[0].filename;
    let url = "/img/" + files[0].filename;

    res.status(200).json(url);
});
router.post("/imgs", imgs.any(), (req, res) => {
    let files = req.files;
    let imgArr = [];
    let length = null;
    for (let i = 0; i < files.length; i++) {
        // let url = 'http://127.0.0.1:8000' + "/img/" + files[i].filename;
        let url = "/img/" + files[i].filename;
        imgArr.push({ url, title: req.body['title' + (i + 1)] });
    }
    length = req.body.stepNum
    res.status(200).json({ data: imgArr, length });
});


// 视频上传接口
const video = multer.diskStorage({
    destination: './api/video',
    filename: function (req, file, cb) {
        let ext = path.extname(file.originalname);
        cb(null, file.fieldname + Date.now() + ext)
    }
})
const videos = multer({ storage: video })
router.post("/videoFile", videos.any(), (req, res) => {
    let files = req.files;
    let url = "/video/" + files[0].filename;
    // let url = 'http://127.0.0.1:8000' + "/video/" + files[0].filename;

    res.status(200).json(url);
});



// 菜谱上传接口
router.post("/memu", async (req, res) => {
    // 获取参数
    const user = req.userInfo._id;
    const { coverImg,
        title,
        material,
        step,
        cookingTime,
        difficulty,
        classList,
        pushTime
    } = req.body;
    let ress = await Update.create({
        coverImg,
        title,
        material,
        step,
        cookingTime,
        difficulty,
        classList,
        pushTime,
        user_id: user,
        type: 1
    });
    //响应客户端
    res.status(200).json({ message: "上传成功" });
});

// 视频上传接口
router.post("/video", async (req, res) => {
    // 获取参数
    const user = req.userInfo._id;
    const { title, con, video, classList, pushTime } = req.body;
    let ress = await Update.create({
        title,
        con,
        video,
        classList,
        pushTime,
        user_id: user,
        type: 2
    });
    //响应客户端
    res.status(200).json({ message: "上传成功" });
});

// 作品上传接口
router.post("/works", async (req, res) => {
    // 获取参数
    const user = req.userInfo._id;
    const { img, title, classList, pushTime } = req.body;
    let ress = await Works.create({
        img,
        title,
        classList,
        pushTime,
        user_id: user
    });
    //响应客户端
    res.status(200).json({ message: "上传成功" });
});


module.exports = router;
